<?php

namespace addons\aliyunemail\controller;

use app\common\controller\Backend;
use think\Exception;

/**
 * 阿里云邮件推送管理
 */
class Index extends Backend
{
    protected $model = null;
    protected $noNeedLogin = ['test'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 测试邮件发送
     */
    public function test()
    {
        $email = $this->request->post('email');
        $code = $this->request->post('code', '1234');
        
        if (!$email) {
            $this->error('请输入邮箱地址');
        }
        
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $this->error('邮箱格式不正确');
        }
        
        try {
            $aliyunemail = new \addons\aliyunemail\library\Aliyunemail();
            $result = $aliyunemail->email($email)->msg("你的邮件验证码是：{$code}")->send();
            
            if ($result) {
                $this->success('邮件发送成功');
            } else {
                $this->error('邮件发送失败：' . $aliyunemail->getError());
            }
        } catch (Exception $e) {
            $this->error('发送异常：' . $e->getMessage());
        }
    }
}
